package org.knife.springbootlearn.security.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 使用内存中验证来实现自定义的登陆认证
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2021年12月01日 星期三 10:20 下午
 * @description
 * @since 1.0
 */
//@Configuration
public class MyInMemoryWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 密码编辑器
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 使用内存存储
        auth.inMemoryAuthentication()
                // 设置密码编码器
            .passwordEncoder(passwordEncoder)
                // 注册用户admin，和密码abc，并给USER和ADMIN角色
            .withUser("admin")
                // passwordEncoder加密abc后的密码
                .password("$2a$10$NDW1rl52HX903fem4GO/zeCMzr0vlMmRtGGVL4CIyB2GO2icQwGr6").roles("USER","ADMIN")
                .and().withUser("myUser")
                // passwordEncoder加密123456后的密码
                .password("$2a$10$QtuLABsEnxdGF0VEoRjjWuRpKxFt9T1J8iFcMXQ8h9QpjISbiq38K").roles("USER");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}
